package com.sinosun.autotestmachine.tools.Excutor;

import java.lang.reflect.InvocationTargetException;

import com.sinosun.autotestmachine.tools.parser.ParamParser;
import com.sinosun.autotestmachine.tools.parser.StepInformation;
import com.sinosun.autotestmachine.tools.proxy.ErrorCode;

import android.text.TextUtils;
import android.util.Log;

public class StepExcutor {

	public static int excuteStep(final Class clazz, final Object obj, final StepInformation stepInformation) {
		Log.e("XXX", "excuteStep step -> 1 stepInformation.getMethodName() " + stepInformation.getMethodName());
		int num = ParamParser.parser(stepInformation.getParam());
		if (!TextUtils.isEmpty(stepInformation.getCacheParam())) {
			num = 2;
		}
		int ret;
		Thread t = null;
		try {
			if (num == 0) {		//不含参数的方法
				ret = (int)clazz.getMethod(stepInformation.getMethodName()).invoke(obj);
			} else if (num == 1) {			//含参数的方法
				ret = (int)clazz.getMethod(stepInformation.getMethodName(), String.class).invoke(obj, stepInformation.getParam());
			} else {
				ret = (int)clazz.getMethod(stepInformation.getMethodName(), String.class, String.class).invoke(obj, stepInformation.getParam(), stepInformation.getCacheParam());
			}
		} catch (NullPointerException e) {
			e.printStackTrace();
			ret = ErrorCode.ERR_UNEXPECT_RESULT;
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
			ret = ErrorCode.ERR_UNEXPECT_RESULT;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			ret = ErrorCode.ERR_UNEXPECT_RESULT;
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			ret = ErrorCode.ERR_UNEXPECT_RESULT;
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			ret = ErrorCode.ERR_UNEXPECT_RESULT;
		}
		Log.e("XXX", "excuteStep step -> 6 ret=" + ret);
		return ret;
	}
}
